/**
 * 地图监控
 * @type {createApplication}
 */
var express = require('express');
var request = require('request');

var url = require('url');
var qstr = require("querystring");
var fs = require("fs");
var pro = require('../../property/cuspro');
var auth = require('../../auth/auth');
var util = require('../../util/commonutil');
var router = express.Router();
// var querystring = require('querystring');

router.get('/', function(req, res, next) {
    auth.checkAuth(req, res, next);
    res.render('map/index',{jw:pro.jw,homeurl:pro.serveraddr,flag: false, totalNum:10,onlineNum:3,onlineNum:7,speedNum:3,stopNum:7,rate:'13%'});
});
router.get('/carmap', function(req, res, next) {
    auth.checkAuth(req, res, next);
    var pathname = url.parse(req.url).pathname;  //pathname => list
    var arg = url.parse(req.url, true).query;   //arg1 => {name:'a',id:'5'}
    var query=req.query;//参数
    var carId=query.carId;
    var jd=query.jd;
    var wd=query.wd;
    res.render('map/newhis',{jw:pro.jw,jd:jd,wd:wd,homeurl:pro.serveraddr,carId:carId});
});
router.get('/cartitle', function(req, res, next) {
    auth.checkAuth(req, res, next);
    //var resdata=[{"totalNum":10,"onlineNum":3,"offlineNum":7,"speedNum":3,"stopNum":7,"rate":'13%'}];
    //var data={"success":true,"data":resdata};
    //res.json(data);




    var arg = url.parse(req.url, true).query;   //arg1 => {name:'a',id:'5'}

    request({
        url: pro.serveraddr+'/realGpsInfo/listByCountCar',
        method: "POST",
        qs: arg
    }, function(error, response, body) {
        if (!error && response.statusCode == 200) {
            var date=JSON.parse(body);
            if(date && date.success){
                res.json(date);
            }else {
                console.log("error= " + date.msg );
                res.json(date.msg);
            }



        }
    });


});
router.get('/warningCl', function(req, res, next) {
    auth.checkAuth(req, res, next);

    var pathname = url.parse(req.url).pathname;  //pathname => list
    var arg = url.parse(req.url, true).query;   //arg1 => {name:'a',id:'5'}
    var query=req.query;//参数
    request({
        url: pro.serveraddr+'/tabWaringInfo/updateType',
        method: "POST",
        qs: arg
    }, function(error, response, body) {
        if (!error && response.statusCode == 200) {
            var data=JSON.parse(body);
            if(data && data.success){
                res.json(data.msg);
            }else if (response.statusCode == 500) {
                res.send("服务错误!");
                console.log("error= 服务错误");
                // res.send();
            }else{
                console.log("error= " + error );
            }
        }
    });


});
router.get('/his', function(req, res, next) {
    auth.checkAuth(req, res, next);
    var pathname = url.parse(req.url).pathname;  //pathname => list
    var arg = url.parse(req.url, true).query;   //arg1 => {name:'a',id:'5'}
    var query=req.query;//参数
    var carId=query.carId;
    var carNo=query.carNo;
    if(!carId){
        carId="0"
    }
    if(!carNo){
        carNo=""
    }

    // res.render('map/sportTrail',{jw:pro.jw,flag: false,serverurl:pro.serveraddr,carId:carId,carNo:carNo});
    res.render('map/newSportTrail',{jw:pro.jw,flag: false,serverurl:pro.serveraddr,carId:carId,carNo:carNo});
});
router.get('/terminals', function(req, res){
    //var rows=[
    //    {"id":1,"baseCarNo":"鲁B12345","gpsDate":"2017-04-05 12:12:00",
    //    "deptName":"市政府","elType":"对象类型-01","accFlag":1,
    //    "online":0,"speed":90,"driection":"正北",
    //    "totalDis":322,"monthDis":8022,"oilDay":20,"oilTotal":220,
    //    "memo":"山东省青岛市高新区松园路-010号"
    //}
    //];
    //var data={"msg":"","recordsFiltered":10,"success":true,"obj":null,"draw":1,"rows":rows,"recordsTotal":10};
    //
    //res.json(data);
     var pathname = url.parse(req.url).pathname;  //pathname => list

     // var arg = url.parse(req.url).query;          //arg => name=a&id=5
     // console.log("Request for " + arg );
     // var str = qstr.parse(arg);                //str=> {name:'a',id:'5'}

     var arg = url.parse(req.url, true).query;   //arg1 => {name:'a',id:'5'}
     // console.log("Request for " + arg1 );

     // var name = qstr.parse(arg).draw;         //name => a
     // console.log("name = "+name);
     var query=req.query;//参数
     // req.params，req.query 和 req.body 中的表单数据
     request({
         url: pro.serveraddr+'/realGpsInfo/listByCar',
         method: "POST",
         qs: arg
     }, function(error, response, body) {
         if (!error && response.statusCode == 200) {
             var date=JSON.parse(body);
             if(date && date.success){
                 res.json(date);
             }else {
                 console.log("error= " + date.msg );
                 res.json(date.msg);
             }



         }
     });
});
router.get('/warnings', function(req, res){
    //var rows=[{"id":1,"baseCarNo":"鲁123","warningDate":"2017-04-05 12:12:00",
    //    "deptName":"市政府","elType":"对象类型-01","warningType":1,
    //    "empName":"王工","type":1,
    //    "memo":"山东省青岛市高新区松园路-010号"
    //}];
    //var data={"msg":"","recordsFiltered":10,"success":true,"obj":null,"draw":1,"rows":rows,"recordsTotal":10}
    //
    //res.json(data);
     var pathname = url.parse(req.url).pathname;  //pathname => list

     // var arg = url.parse(req.url).query;          //arg => name=a&id=5
     // console.log("Request for " + arg );
     // var str = qstr.parse(arg);                //str=> {name:'a',id:'5'}

     var arg = url.parse(req.url, true).query;   //arg1 => {name:'a',id:'5'}
     // console.log("Request for " + arg1 );

     // var name = qstr.parse(arg).draw;         //name => a
     // console.log("name = "+name);
     var query=req.query;//参数
     // req.params，req.query 和 req.body 中的表单数据
     request({
         url: pro.serveraddr+'/realGpsInfo/listByCarWaring',
         method: "POST",
         qs: arg
     }, function(error, response, body) {
         if (!error && response.statusCode == 200) {
             var date=JSON.parse(body);
             if(date && date.success){
                 res.json(date);
             }else {
                 console.log("error= " + date.msg );
                 res.json(date.msg);
             }



         }
     });
});
router.get('/hiswarnings', function(req, res){
    //var rows=[{"id":1,"baseCarNo":"鲁123","warningDate":"2017-04-05 12:12:00",
    //    "deptName":"市政府","elType":"对象类型-01","warningType":1,
    //    "empName":"王工","type":1,
    //    "memo":"山东省青岛市高新区松园路-010号"
    //}];
    //var data={"msg":"","recordsFiltered":10,"success":true,"obj":null,"draw":1,"rows":rows,"recordsTotal":10}
    //
    //res.json(data);
     var pathname = url.parse(req.url).pathname;  //pathname => list

     // var arg = url.parse(req.url).query;          //arg => name=a&id=5
     // console.log("Request for " + arg );
     // var str = qstr.parse(arg);                //str=> {name:'a',id:'5'}

     var arg = url.parse(req.url, true).query;   //arg1 => {name:'a',id:'5'}
     // console.log("Request for " + arg1 );

     // var name = qstr.parse(arg).draw;         //name => a
     // console.log("name = "+name);
     var query=req.query;//参数
     // req.params，req.query 和 req.body 中的表单数据
     request({
         url: pro.serveraddr+'/hisGpsInfo/queryTableWaringGpsData',
         method: "POST",
         qs: arg
     }, function(error, response, body) {
         if (!error && response.statusCode == 200) {
             var date=JSON.parse(body);
             if(date && date.success){
                 res.json(date);
             }else {
                 console.log("error= " + date.msg );
                 res.json(date.msg);
             }



         }
     });
});
router.post('/cars', function(req, res){
    // var pathname = url.parse(req.url).pathname;  //pathname => list
    // var arg = url.parse(req.url, true).query;   //arg1 => {name:'a',id:'5'}
    //var query=req.query;//参数
    //var obj=req.body;
    //var carno="鲁B12345";
    //var carno1="鲁B133333";
    //var lng=120.400009;
    //var lng1=120.420922;
    //var lat=36.187533;
    //var lat1=36.201048;
    //var id=query.id;
    //var carError=1;
    //var resdata=[{"carno":carno,"lng":lng,"lat":lat,"direction":0,location:"青岛市李沧区文昌路",carError:carError}];
    //
    //if(obj&&obj.carIds && obj.carIds=="101"){
    //    resdata=[{"carno":carno,"lng":lng,"lat":lat,"direction":0,location:"青岛市李沧区文昌路",carError:carError}];
    //
    //}else if(obj&&obj.carIds && obj.carIds=="102"){
    //    resdata=[{"carno":carno1,"lng":lng1,"lat":lat1,"direction":0,location:"青岛市李沧区文昌路1",carError:carError}];
    //
    //}
    //var data={"success":true,"data":resdata};
    //res.json(data);
     request({
         url: pro.serveraddr+'/realGpsInfo/listByTrackCar',
         method: "POST",
         qs: req.body
     }, function(error, response, body) {
         if (!error && response.statusCode == 200) {
             var date=JSON.parse(body);
             if(date ){//&& date.success
                 res.json(date);
             }else {
                 console.log("error= " + date.msg );
                 res.json(date.msg);
             }
         }
     });
});
//获取carid的当月里程数据
router.post('/cardatas', function(req, res){
    //var carId=req.body.carId;
    //var data=[];
    //if(carId){
    //    var resdata=[{"start":"2017-05-06","mile":"111"}];
    //    data={"success":true,"carId":1,"month":"2017-05","data":resdata};
    //}
    //res.json(data);
     request({
         url: pro.serveraddr+'/hisGpsInfo/queryByDay',
         method: "POST",
         json: true,
         headers: {
             "content-type": "application/json",
         },
         qs: req.body
     }, function(error, response, body) {
         if (!error && response.statusCode == 200) {
             var info = body;
             res.json(info);
             // res.send();
         }else if (response.statusCode == 500) {
             res.send("服务错误!");
             console.log("error= 服务错误");
             // res.send();
         }else{
             console.log("error= " + error );
         }

     });
});
router.post('/carMonth', function(req, res){
    //var carId=req.body.carId;
    //var data=[];
    //if(carId){
    //    var resdata=[{"start":"2017-05-06","mile":"111"}];
    //    data={"success":true,"carId":1,"month":"2017-05"};
    //}
    //res.json(data);
     request({
         url: pro.serveraddr+'/hisGpsInfo/queryByMonth',
         method: "POST",
         json: true,
         headers: {
             "content-type": "application/json",
         },
         qs: req.body
     }, function(error, response, body) {
         if (!error && response.statusCode == 200) {
             var info = body;
             res.json(info.data);
             // res.send();
         }else if (response.statusCode == 500) {
             res.send("服务错误!");
             console.log("error= 服务错误");
             // res.send();
         }else{
             console.log("error= " + error );
         }

     });
});
router.get('/carHisData', function(req, res){
    var pathname = url.parse(req.url).pathname;  //pathname => list


    var arg = url.parse(req.url, true).query;   //arg1 => {name:'a',id:'5'}

    var query=req.query;//参数
     request({
         url: pro.serveraddr+'/hisGpsInfo/queryTableMapGpsData',
         method: "POST",
         json: true,
         headers: {
             "content-type": "application/json",
         },
         qs: arg
     }, function(error, response, body) {
         if (!error && response.statusCode == 200) {
             var info = body;
             res.json(info);
             // res.send();
         }else if (response.statusCode == 500) {
             res.send("服务错误!");
             console.log("error= 服务错误");
             // res.send();
         }else{
             console.log("error= " + error );
         }

     });
});
router.get('/carTabHisData', function(req, res){
    var pathname = url.parse(req.url).pathname;  //pathname => list


    var arg = url.parse(req.url, true).query;   //arg1 => {name:'a',id:'5'}

    var query=req.query;//参数
     request({
         url: pro.serveraddr+'/hisGpsInfo/queryTableGpsData',
         method: "POST",
         json: true,
         headers: {
             "content-type": "application/json",
         },
         qs: arg
     }, function(error, response, body) {
         if (!error && response.statusCode == 200) {
             var info = body;
             res.json(info);
             // res.send();
         }else if (response.statusCode == 500) {
             res.send("服务错误!");
             console.log("error= 服务错误");
             // res.send();
         }else{
             console.log("error= " + error );
         }

     });
});
router.post('/saveFence', function(req, res){
    // req.params，req.query 和 req.body 中的表单数据
    request({
        url: pro.serveraddr+'/baseCommonFence/saveFence',
        method: "POST",
        json: true,
        headers: {
            "content-type": "application/json",
        },
        qs: req.body
    }, function(error, response, body) {
        if (!error && response.statusCode == 200) {
            var info = body;
            res.json(info);
            // res.send();
        }else if (response.statusCode == 500) {
            res.send("服务错误!");
            console.log("error= 服务错误");
            // res.send();
        }else{
            console.log("error= " + error );
        }

    });
});
router.post("/listByAreaCars",function (req, res) {
    // req.params，req.query 和 req.body 中的表单数据
    request({
        url: pro.serveraddr+'/realGpsInfo/listByAreaCars',
        method: "POST",
        json: true,
        headers: {
            "content-type": "application/json",
        },
        qs: req.body
    }, function(error, response, body) {
        if (!error && response.statusCode == 200) {
            var info = body;
            res.json(info);
            // res.send();
        }else if (response.statusCode == 500) {
            res.send("服务错误!");
            console.log("error= 服务错误");
            // res.send();
        }else{
            console.log("error= " + error );
        }

    });
});


module.exports = router;
